package cn.desk.fileManager.entity;

import org.apache.struts2.ServletActionContext;

import cn.desk.commons.util.StringUtil;


public class TreeNode implements Comparable<TreeNode>{
	private String parent;
	private String name;
	private boolean dir;
	/**
	 * @return the parent
	 */
	public String getParent() {
		if(parent==null){
			return "";
		}
		return parent;
	}

	/**
	 * @param parent
	 *            the parent to set
	 */
	public void setParent(String parent) {
		this.parent = parent;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	public String getDisplayName(){
		return StringUtil.displayShort(name, 10);
	}
	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param dir the dir to set
	 */
	public void setDir(boolean dir) {
		this.dir = dir;
	}

	/**
	 * @return the dir
	 */
	public boolean isDir() {
		return dir;
	}
	
	public String getIcon(){
		String fileExt;
		if (dir) {
			fileExt = "FOLDER";
		} else {
			int point=name.lastIndexOf(".");
			if(point<0){
				fileExt="DEFAULT";
			}else{
				fileExt = name.substring(point+1);
			}
		}
		Object icon=ServletActionContext.getRequest().getSession().getAttribute("FILEICON_"+fileExt.toUpperCase());
		if(icon==null){
			icon=ServletActionContext.getRequest().getSession().getAttribute("FILEICON_DEFAULT");
			return String.valueOf(icon);
		}
		return String.valueOf(icon);
	}
	public int compareTo(TreeNode o) {
		if(o == null){
			return -1;
		}
		if(this.isDir() && o.isDir()){
			return getName().compareToIgnoreCase(o.getName());
		}
		if(this.isDir()&& !o.isDir()){
			return -1;
		}
		return 1;
	}
}
